package com.liugh.service.zhongche.Imp;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardMessage;
import com.liugh.mapper.zhongche.StandardMessageMapper;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.IStandardMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StandardMessageImp extends ServiceImpl<StandardMessageMapper, StandardMessage> implements IStandardMessageService {
    @Autowired
    IUserService userService;

    @Autowired
    StandardMessageMapper messageMapper;


    @Override
    public StandardMessage addMessage(StandardMessage message) {

//        if (CommonUtil.isDBOpSuccess(messageMapper.insert(message))) {
//            message.setUserName(userService.getUserByUserNo(message.getUserNo()).getUsername());
//        }
        return insert(message) ? message : null;
    }

    @Override
    public boolean deleteMessageById(int id) {
        return delete(new EntityWrapper<StandardMessage>().where("id = {0}", id));
    }

    @Override
    public Page<StandardMessage> getAllMsgPage(Page<StandardMessage> page) {
        List<StandardMessage> messages = messageMapper.selectPage(page,new EntityWrapper<StandardMessage>().orderBy("create_time",false));
        for (StandardMessage message : messages) {
            message.setUserName(userService.getUserByUserNo(message.getUserNo()).getUsername());
        }
        page.setRecords(messages);
        return page;
    }


    /**
     * 根据id删除我的留言 .
     * @param id .
     * @param user .
     * @return .
     */
    public boolean delMessage(int id, User user) {
        return delete(new EntityWrapper<StandardMessage>().eq("id", id).eq("user_no", user.getUserNo()));
    }


    /**
     * 根据id来更新 .
     * @param standardMessage .
     * @return .
     */
    @Override
    public StandardMessage updateMessageById(StandardMessage standardMessage) {
        return updateById(standardMessage) ? standardMessage : null;
    }


    /**
     * 根据id查找 .
     * @param id .
     * @return .
     */
    public StandardMessage getMessageById(int id) {
        StandardMessage message = selectById(id);
        message.setUserName(userService.getUserByUserNo(message.getUserNo()).getUsername());
        return message;
    }


    /**
     * 获取我的留言
     * @param page
     * @param userNo
     * @param isAsc
     * @return
     */
    @Override
    public Page<StandardMessage> getMyMsgPage(Page<StandardMessage> page, String userNo,boolean isAsc) {
       List<StandardMessage>  messages = messageMapper.selectPage(page,new EntityWrapper<StandardMessage>().where("user_no = {0}",userNo).orderBy("create_time",isAsc));
        for (StandardMessage message : messages) {
            message.setUserName(userService.getUserByUserNo(userNo).getUsername());
        }
        page.setRecords(messages);
        return page;
    }
}
